package com.example.librarymanagementproject2.Common.aop;

import com.example.librarymanagementproject2.Common.config.RabbitConfig;
import com.example.librarymanagementproject2.Common.exception.GlobalException;
import com.example.librarymanagementproject2.converter.BookConverter;
import com.example.librarymanagementproject2.mapper.BookMapper;
import com.example.librarymanagementproject2.model.document.BookDocument;
import com.example.librarymanagementproject2.model.dto.BookWithCategoryDTO;
import com.example.librarymanagementproject2.model.entity.Book;
import com.example.librarymanagementproject2.model.entity.BookCategory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName BookAspect
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/27 19:24
 * @Version 1.0
 */
@Aspect
@Component
public class BookAspect {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookConverter bookConverter;
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    /**
     * @author: kx
     * @date: 2025/10/27 19:27
     * @param joinPoint : 切入点
     * @description: 插入图书信息后置通知
    */
    @AfterReturning(value = "execution(* com.example.librarymanagementproject2.service.impl.BookServiceImpl.insertBook(..))")
    public void afterInsertBook(JoinPoint joinPoint) {
        try {
            // 获取方法所有参数
            Object[] args = joinPoint.getArgs();

            // 获取图书信息
            Book book = (Book) args[0];

            // 获取图书分类信息
            BookCategory bookCategory = bookMapper.getBookCategoryById(book.getCategoryId());
            if (bookCategory == null) {
                throw new GlobalException(500, "图书分类不存在");
            }
            // 构建图书和图书分类数据传输信息
            BookWithCategoryDTO bookWithCategoryDTO = new BookWithCategoryDTO();
            bookWithCategoryDTO.setBook(book);
            bookWithCategoryDTO.setBookCategory(bookCategory);

            // 创建消息唯一ID
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            // 投递消息
            rabbitTemplate.convertAndSend(RabbitConfig.BOOK_EXCHANGE, RabbitConfig.BOOK_INSERT_ROUTING_KEY, bookWithCategoryDTO, correlationData);
        }catch (Exception e) {
            throw new GlobalException(500, "投递消息失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/10/28 19:37
     * @param joinPoint : 切入点
     * @description: 修改图书状态成功后置通知
     */
    @AfterReturning(value = "execution(* com.example.librarymanagementproject2.service.impl.BookServiceImpl.updateBookStatus(..))")
    public void afterUpdateBookStatusUpdateIndex(JoinPoint joinPoint) {
        // 获取所有参数
        Object[] args = joinPoint.getArgs();
        // 获取图书信息
        Book book = (Book) args[0];

        // 从数据库获取最新的数据
        Book book1 = bookMapper.getBookById(book.getId());
        // 获取图书分类信息
        BookCategory bookCategory = bookMapper.getBookCategoryById(book1.getCategoryId());

        // 构建图书和图书分类数据传输信息
        BookWithCategoryDTO bookWithCategoryDTO = new BookWithCategoryDTO();
        bookWithCategoryDTO.setBook(book1);
        bookWithCategoryDTO.setBookCategory(bookCategory);

        // 创建消息唯一ID
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        // 投递消息
        rabbitTemplate.convertAndSend(RabbitConfig.BOOK_EXCHANGE, RabbitConfig.BOOK_OPERATION_ROUTING_KEY, bookWithCategoryDTO, correlationData);
    }
}
